package com.ruoyi.basebill.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.basebill.DTO.FlfjtInfo;
import com.ruoyi.basebill.DTO.FlfjtEntryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.WfzFlfjtentry;
import com.ruoyi.basebill.mapper.WfzFlfjtMapper;
import com.ruoyi.basebill.domain.WfzFlfjt;
import com.ruoyi.basebill.service.IWfzFlfjtService;

/**
 * 福利费计提Service业务层处理
 * 
 * @author xiefeng
 * @date 2022-12-19
 */
@Service
public class WfzFlfjtServiceImpl implements IWfzFlfjtService 
{
    @Autowired
    private WfzFlfjtMapper wfzFlfjtMapper;

    /**
     * 查询福利费计提
     * 
     * @param fid 福利费计提主键
     * @return 福利费计提
     */
    @Override
    public WfzFlfjt selectWfzFlfjtByFid(Long fid)
    {
        return wfzFlfjtMapper.selectWfzFlfjtByFid(fid);
    }

    /**
     * 查询福利费计提列表
     * 
     * @param wfzFlfjt 福利费计提
     * @return 福利费计提
     */
    @Override
    public List<WfzFlfjt> selectWfzFlfjtList(WfzFlfjt wfzFlfjt)
    {
        return wfzFlfjtMapper.selectWfzFlfjtList(wfzFlfjt);
    }

    /**
     * 新增福利费计提
     * 
     * @param wfzFlfjt 福利费计提
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWfzFlfjt(WfzFlfjt wfzFlfjt)
    {
        int rows = wfzFlfjtMapper.insertWfzFlfjt(wfzFlfjt);
        insertWfzFlfjtentry(wfzFlfjt);
        return rows;
    }

    /**
     * 修改福利费计提
     * 
     * @param wfzFlfjt 福利费计提
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWfzFlfjt(WfzFlfjt wfzFlfjt)
    {
        wfzFlfjtMapper.deleteWfzFlfjtentryByFparentid(wfzFlfjt.getFid());
        insertWfzFlfjtentry(wfzFlfjt);
        return wfzFlfjtMapper.updateWfzFlfjt(wfzFlfjt);
    }

    /**
     * 批量删除福利费计提
     * 
     * @param fids 需要删除的福利费计提主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzFlfjtByFids(Long[] fids)
    {
        wfzFlfjtMapper.deleteWfzFlfjtentryByFparentids(fids);
        return wfzFlfjtMapper.deleteWfzFlfjtByFids(fids);
    }

    /**
     * 删除福利费计提信息
     * 
     * @param fid 福利费计提主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWfzFlfjtByFid(Long fid)
    {
        wfzFlfjtMapper.deleteWfzFlfjtentryByFparentid(fid);
        return wfzFlfjtMapper.deleteWfzFlfjtByFid(fid);
    }

    /**
     * 新增福利费计提分录信息
     * 
     * @param wfzFlfjt 福利费计提对象
     */
    public void insertWfzFlfjtentry(WfzFlfjt wfzFlfjt)
    {
        List<WfzFlfjtentry> wfzFlfjtentryList = wfzFlfjt.getWfzFlfjtentryList();
        Long fid = wfzFlfjt.getFid();
        if (StringUtils.isNotNull(wfzFlfjtentryList))
        {
            List<WfzFlfjtentry> list = new ArrayList<WfzFlfjtentry>();
            for (WfzFlfjtentry wfzFlfjtentry : wfzFlfjtentryList)
            {
                wfzFlfjtentry.setFparentid(fid);
                list.add(wfzFlfjtentry);
            }
            if (list.size() > 0)
            {
                wfzFlfjtMapper.batchWfzFlfjtentry(list);
            }
        }
    }

    /**
     * 导入福利费计提
     * @param wfzFlfjt
     * @return
     */
    @Override
    public String importFlfjt(WfzFlfjt wfzFlfjt) {
        FlfjtInfo flfjtInfo = new FlfjtInfo();
        String fnumber = wfzFlfjt.getFnumber();//单据编号
        String fcompany = wfzFlfjt.getFcompany();//组织（公司）
        String fperiod = wfzFlfjt.getFperiod();//薪酬期间
        String fvourtype = wfzFlfjt.getFvourtype();//凭证字

        flfjtInfo.setNumber(fnumber);
        flfjtInfo.setCompany(fcompany);
        flfjtInfo.setPeriod(fperiod);
        flfjtInfo.setVourType(fvourtype);
        flfjtInfo.setSourcebillid(String.valueOf(wfzFlfjt.getFid()));

        List<WfzFlfjtentry> wfzFlfjtentryList = wfzFlfjt.getWfzFlfjtentryList();
        ArrayList<FlfjtEntryInfo> entry = new ArrayList<>();
        //组装分录数据
        for (int i = 0; i < wfzFlfjtentryList.size(); i++) {
            WfzFlfjtentry wfzFlfjtentry = wfzFlfjtentryList.get(i);
            String fcostnumber = wfzFlfjtentry.getFcostnumber();//成本中心
            BigDecimal fjrfl = wfzFlfjtentry.getFjrfl();//节日福利
            BigDecimal fgwbn = wfzFlfjtentry.getFgwbn();//高温保暖
            BigDecimal ffhbfl = wfzFlfjtentry.getFfhbfl();//非货币福利
            String fcostcompany = wfzFlfjtentry.getFcostcompany();//成本中心所属财务组织
            String fjzcompany = wfzFlfjtentry.getFjzcompany();//记账公司
            String fjsfs = wfzFlfjtentry.getFjsfs();//组织间结算方式
            String fcbzxxz = wfzFlfjtentry.getFcbzxxz();//成本中心属性
            String fvourtype1 = wfzFlfjtentry.getFvourtype();//凭证字

            FlfjtEntryInfo flfjtentryInfo = new FlfjtEntryInfo();
            flfjtentryInfo.setCostCenter(fcostnumber);
            flfjtentryInfo.setJrfl(fjrfl);
            flfjtentryInfo.setGwbn(fgwbn);
            flfjtentryInfo.setFhbfl(ffhbfl);
            flfjtentryInfo.setCostcompany(fcostcompany);
            flfjtentryInfo.setJzcompany(fjzcompany);
            flfjtentryInfo.setJsfs(fjsfs);
            flfjtentryInfo.setCbzxxz(fcbzxxz);
            flfjtentryInfo.setVourType(fvourtype1);

            entry.add(flfjtentryInfo);
        }

        flfjtInfo.setEntry(entry);

        //转为Json

        String json = JSONObject.toJSONString(flfjtInfo);

        return json;
    }
}
